package TestProject01;
//学生管理系统（自己写的）

import java.util.ArrayList;
import java.util.Scanner;

public class StudentStudentSystemTest {
    public static void main(String[] args) {
        //定义集合用于存储学生对象
        ArrayList<StudentStudentSystem> list = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            //设计系统界面
            System.out.println("----------欢迎来到学生管理系统----------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5：退出");
            System.out.println("请输入你的选择：");
            String choose = sc.next();
            switch (choose) {
                case "1"://添加学生
                    addStudent(list);
                    break;
                case "2"://删除学生
                    deletStudent(list);
                    break;
                case "3"://修改学生
                    updateStudent(list);
                    break;
                case "4"://查询学生
                    queryStudent(list);
                    break;
                case "5"://退出
                    //break loop;
                    System.exit(0);
                default:
                    System.out.println("没有这个选项，请重新输入：");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<StudentStudentSystem> list) {
        Scanner sc = new Scanner(System.in);

        StudentStudentSystem stu = new StudentStudentSystem();

        System.out.println("请输入学生id：");
        while (true) {
            String id = sc.next();
            int index = getIndex(list, id);
            if (index >= 0) {
                System.out.println("此id已存在，请重新输入：");
            } else {
                stu.setId(id);
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        stu.setName(name);

        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        stu.setAge(age);

        System.out.println("请输入学生家庭住址：");
        String address = sc.next();
        stu.setAddress(address);

        list.add(stu);
        System.out.println("学生添加成功！");

    }

    //删除学生
    public static void deletStudent(ArrayList<StudentStudentSystem> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的id：");
        String id = sc.next();

        for (int i = 0; i < list.size(); i++) {
            StudentStudentSystem stu = list.get(i);
            String sid = stu.getId();
            if (id.equals(sid)) {
                int index = getIndex(list, id);
                list.remove(index);
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("此id不存在！");
    }

    //修改学生
    public static void updateStudent(ArrayList<StudentStudentSystem> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id：");
        String id = sc.next();

        for (int i = 0; i < list.size(); i++) {
            StudentStudentSystem stu = list.get(i);
            String sid = stu.getId();
            if (id.equals(sid)) {
                System.out.println("请输入学生姓名：");
                String name = sc.next();
                stu.setName(name);

                System.out.println("请输入学生年龄：");
                int age = sc.nextInt();
                stu.setAge(age);

                System.out.println("请输入学生家庭住址：");
                String address = sc.next();
                stu.setAddress(address);

                System.out.println("学生信息修改成功！");
                return;
            }
        }
        System.out.println("此id不存在！");
    }

    //查询学生
    public static void queryStudent(ArrayList<StudentStudentSystem> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息，请添加后再查询！");
            return;
        }

        //打印标题
        System.out.println("id" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "家庭住址");
        for (int i = 0; i < list.size(); i++) {
            StudentStudentSystem stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAddress() + "\t" + stu.getAddress());
        }
    }

    //判断id在集合中是否存在
    public static boolean contains(ArrayList<StudentStudentSystem> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            StudentStudentSystem stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)) {
                return true;
            }
        }
        return false;
    }

    //根据集合获取id的索引
    public static int getIndex(ArrayList<StudentStudentSystem> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            StudentStudentSystem stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
